package managedbean.event;

import entity.ActivityAddressEntity;
import entity.ActivityOrganiserEntity;
import entity.EventEntity;
import entity.EventTimeEntity;
import entity.Ticket;
import exception.ExistException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import javax.servlet.http.HttpSession;
import session.stateless.EventSessionBean;
import session.stateless.StakeholderSessionBeanStateless;
import session.stateless.SystemMessagesBean;
import util.entityhelper.StakeholderAccountState;
import util.enumeration.ActivityType;
import util.localization.WebLocalization;

/**
 *
 * @author KohLi
 */
@ManagedBean
@ViewScoped
public class ViewEventManagedBean {

    /**
     * @return the logger
     */
    public static Logger getLogger() {
        return logger;
    }

    /**
     * @param aLogger the logger to set
     */
    public static void setLogger(Logger aLogger) {
        logger = aLogger;
    }
    //private PageRedirectorBean pageRedictor = new PageRedirectorBean();
    private String name;
    private Long stakeholderId;
    private EventEntity eventActivity;
    private EventTimeEntity eventTime;
    private Ticket ticket;
    private ActivityOrganiserEntity organiser;
    private ActivityAddressEntity address;
    private Long staffAccountId;
    private String nextPage;
    private FacesContext ctx = FacesContext.getCurrentInstance();
    @EJB
    private EventSessionBean eventSessionBean;
    @EJB
    private SystemMessagesBean systemMessages;
    @EJB
    private StakeholderSessionBeanStateless stakeholderSessionBean;
    private String preferredLocale;
    private WebLocalization localization;
    private StakeholderAccountState stakeholder;
    private ActivityType activityType;
    private boolean skip;
    private static Logger logger = Logger.getLogger(EventEntity.class.getName());
    private long eventId;
    private List<EventEntity> eventsList;

    /** Creates a new instance of ViewEventManagedBean */
    public ViewEventManagedBean() {
        // get account ID values from session
        if (ctx.getExternalContext().getSessionMap().containsKey("stakeholderId")) {
            stakeholderId = (Long) ctx.getExternalContext().getSessionMap().get("stakeholderId");
            // put back the session IDs taken
            ctx.getExternalContext().getSessionMap().put("stakeholderId", stakeholderId);
        } else if (ctx.getExternalContext().getSessionMap().containsKey("staffAccountId")) {
            // get session values
            staffAccountId = (Long) ctx.getExternalContext().getSessionMap().get("staffAccountId");
            // put back session values
            ctx.getExternalContext().getSessionMap().put("staffAccountId", staffAccountId);
        } else {
            // redirect back to login
        }
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */


    }

    public void initView(PhaseEvent event) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if (ctx.getExternalContext().getSessionMap().containsKey("successmsg")) {
            String statusMessage = (String) ctx.getExternalContext().getSessionMap().get("successmsg");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("successmsg");
        }
        //Clear previous set messages(delete)
        FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();

        // setStakeholderId((Long) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderId"));
        try {
            stakeholder = stakeholderSessionBean.getStakeholderPortalInfoById(stakeholderId);
        } catch (ExistException ex) {
            ex.getMessage();
        }
        if (getStakeholderId() == null) {

            //Session not save. (redirect Stakeholder to login.
            String nextPage = FacesContext.getCurrentInstance().getExternalContext().encodeActionURL(FacesContext.getCurrentInstance().getApplication().
                    getViewHandler().getActionURL(FacesContext.getCurrentInstance(), "/StakeholderLogin.xhtml"));
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect(nextPage);
            } catch (IOException ioe) {
                ioe.printStackTrace();

            }

        } else {

            getAllEvent();

        }
    }

    public void redirectPage(ActionEvent event) throws UnsupportedEncodingException, NoSuchAlgorithmException {


        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("stakeholderId", stakeholderId);
        FacesContext ctx = FacesContext.getCurrentInstance();
        String nextPage = ctx.getExternalContext().encodeActionURL(ctx.getApplication().getViewHandler().
                getActionURL(ctx, "/common/AddEvent.xhtml"));
        try {
            ctx.getExternalContext().redirect(nextPage);
        } catch (IOException ex) {
            //file dont exist.
        }

    }

    public void getAllEvent() {

        eventsList = new ArrayList<EventEntity>();
        eventsList = eventSessionBean.getEventByStakeholderName(stakeholder.getCorpName());
    }

    public void view_Edit_Event(EventEntity e) throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectEvent", eventSessionBean.getEventByEventId(e.getId()));
        System.out.println("at the viewAllEvent page" + e.getEventName());
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("stakeholderId", stakeholderId);
        FacesContext ctx = FacesContext.getCurrentInstance();
        String nextPage = ctx.getExternalContext().encodeActionURL(ctx.getApplication().getViewHandler().
                getActionURL(ctx, "/common/EditEvent.xhtml"));
        try {
            ctx.getExternalContext().redirect(nextPage);
        } catch (IOException ex) {
            //file dont exist.
        }

    }
    
     public void view_Del_Event(EventEntity e) throws IOException {
          FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectEvent", eventSessionBean.getEventByEventId(e.getId()));
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("delete", true);
        System.out.println("at the viewAllEvent page" + e.getEventName());
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("stakeholderId", stakeholderId);
        FacesContext ctx = FacesContext.getCurrentInstance();
        String nextPage = ctx.getExternalContext().encodeActionURL(ctx.getApplication().getViewHandler().
                getActionURL(ctx, "/common/ViewSEvent.xhtml"));
        try {
            ctx.getExternalContext().redirect(nextPage);
        } catch (IOException ex) {
            //file dont exist.
        }
         
     }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the stakeholderId
     */
    public Long getStakeholderId() {
        return stakeholderId;
    }

    /**
     * @param stakeholderId the stakeholderId to set
     */
    public void setStakeholderId(Long stakeholderId) {
        this.stakeholderId = stakeholderId;
    }

    /**
     * @return the eventActivity
     */
    public EventEntity getEventActivity() {
        return eventActivity;
    }

    /**
     * @param eventActivity the eventActivity to set
     */
    public void setEventActivity(EventEntity eventActivity) {
        this.eventActivity = eventActivity;
    }

    /**
     * @return the eventTime
     */
    public EventTimeEntity getEventTime() {
        return eventTime;
    }

    /**
     * @param eventTime the eventTime to set
     */
    public void setEventTime(EventTimeEntity eventTime) {
        this.eventTime = eventTime;
    }

    /**
     * @return the ticket
     */
    public Ticket getTicket() {
        return ticket;
    }

    /**
     * @param ticket the ticket to set
     */
    public void setTicket(Ticket ticket) {
        this.ticket = ticket;
    }

    /**
     * @return the organiser
     */
    public ActivityOrganiserEntity getOrganiser() {
        return organiser;
    }

    /**
     * @param organiser the organiser to set
     */
    public void setOrganiser(ActivityOrganiserEntity organiser) {
        this.organiser = organiser;
    }

    /**
     * @return the address
     */
    public ActivityAddressEntity getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(ActivityAddressEntity address) {
        this.address = address;
    }

    /**
     * @return the staffAccountId
     */
    public Long getStaffAccountId() {
        return staffAccountId;
    }

    /**
     * @param staffAccountId the staffAccountId to set
     */
    public void setStaffAccountId(Long staffAccountId) {
        this.staffAccountId = staffAccountId;
    }

    /**
     * @return the nextPage
     */
    public String getNextPage() {
        return nextPage;
    }

    /**
     * @param nextPage the nextPage to set
     */
    public void setNextPage(String nextPage) {
        this.nextPage = nextPage;
    }

    /**
     * @return the ctx
     */
    public FacesContext getCtx() {
        return ctx;
    }

    /**
     * @param ctx the ctx to set
     */
    public void setCtx(FacesContext ctx) {
        this.ctx = ctx;
    }

    /**
     * @return the eventSessionBean
     */
    public EventSessionBean getEventSessionBean() {
        return eventSessionBean;
    }

    /**
     * @param eventSessionBean the eventSessionBean to set
     */
    public void setEventSessionBean(EventSessionBean eventSessionBean) {
        this.eventSessionBean = eventSessionBean;
    }

    /**
     * @return the systemMessages
     */
    public SystemMessagesBean getSystemMessages() {
        return systemMessages;
    }

    /**
     * @param systemMessages the systemMessages to set
     */
    public void setSystemMessages(SystemMessagesBean systemMessages) {
        this.systemMessages = systemMessages;
    }

    /**
     * @return the stakeholderSessionBean
     */
    public StakeholderSessionBeanStateless getStakeholderSessionBean() {
        return stakeholderSessionBean;
    }

    /**
     * @param stakeholderSessionBean the stakeholderSessionBean to set
     */
    public void setStakeholderSessionBean(StakeholderSessionBeanStateless stakeholderSessionBean) {
        this.stakeholderSessionBean = stakeholderSessionBean;
    }

    /**
     * @return the preferredLocale
     */
    public String getPreferredLocale() {
        return preferredLocale;
    }

    /**
     * @param preferredLocale the preferredLocale to set
     */
    public void setPreferredLocale(String preferredLocale) {
        this.preferredLocale = preferredLocale;
    }

    /**
     * @return the localization
     */
    public WebLocalization getLocalization() {
        return localization;
    }

    /**
     * @param localization the localization to set
     */
    public void setLocalization(WebLocalization localization) {
        this.localization = localization;
    }

    /**
     * @return the stakeholder
     */
    public StakeholderAccountState getStakeholder() {
        return stakeholder;
    }

    /**
     * @param stakeholder the stakeholder to set
     */
    public void setStakeholder(StakeholderAccountState stakeholder) {
        this.stakeholder = stakeholder;
    }

    /**
     * @return the activityType
     */
    public ActivityType getActivityType() {
        return activityType;
    }

    /**
     * @param activityType the activityType to set
     */
    public void setActivityType(ActivityType activityType) {
        this.activityType = activityType;
    }

    /**
     * @return the skip
     */
    public boolean isSkip() {
        return skip;
    }

    /**
     * @param skip the skip to set
     */
    public void setSkip(boolean skip) {
        this.skip = skip;
    }

    /**
     * @return the eventId
     */
    public long getEventId() {
        return eventId;
    }

    /**
     * @param eventId the eventId to set
     */
    public void setEventId(long eventId) {
        this.eventId = eventId;
    }

    /**
     * @return the eventsList
     */
    public List<EventEntity> getEventsList() {
        return eventsList;
    }

    /**
     * @param eventsList the eventsList to set
     */
    public void setEventsList(List<EventEntity> eventsList) {
        this.eventsList = eventsList;
    }
}
